;;; ~/.doom.d/config/init-python-mode.el -*- lexical-binding: t; -*-

;; set anaconda's ipython as default python interpreter
(setq python-shell-interpreter "/opt/anaconda/bin/ipython")


;; run IPython interpreter
(defun Hesperus-ipython ()
  "Open Python interpreter in Emacs terminal."
  (interactive)
  (Hesperus-split-window-right)
  (ansi-term "/opt/anaconda/bin/ipython"))

(bind-key* "C-c I" 'Hesperus-ipython)
(bind-key* "s-I" 'Hesperus-ipython)


(defun Hesperus-py-run ()
  "Run .py file."
  (interactive)
  (shell-command
   (format "%s %s" python-shell-interpreter
           (buffer-file-name (current-buffer)))))

(defun Hesperus-py-send-line ()
  "将当前行发送到 python shell.
发送前会对删除前后空格，主要使用场景是将 doctest 代码发送到 shell 进行测试。"
  (interactive)
  (python-shell-send-string (string-trim (thing-at-point 'line))))

(add-hook 'python-mode-hook
          (lambda ()
            ;; submit current buffer
            (local-set-key (kbd "<f8>") 'Hesperus-py-run)
            (local-set-key (kbd "C-x C-e") 'Hesperus-py-send-line)
            (electric-operator-mode)
            (setq lsp-clients-python-library-directories '("/usr/bin/" "/opt/anaconda/bin/" "/usr/local/" "/usr/"))))


(provide 'init-python)
